<!-- Sharing Permissions -->

<%# initialization: required to keep javascript independent of the access type codes, to allow the use the constants instead -%>
<input type="hidden" id="const_private" value="<%= Policy::PRIVATE -%>">
<input type="hidden" id="const_everyone" value="<%= Policy::EVERYONE -%>">
<input type="hidden" id="const_all_registered_users" value="<%= Policy::ALL_REGISTERED_USERS -%>">
<input type="hidden" id="const_all_elab_users" value="<%= Policy::ALL_ELAB_USERS -%>">
<input type="hidden" id="const_custom_permissions_only" value="<%= Policy::CUSTOM_PERMISSIONS_ONLY -%>">

<input type="hidden" id="const_determined_by_group" value="<%= Policy::DETERMINED_BY_GROUP -%>">
<input type="hidden" id="const_no_access" value="<%= Policy::NO_ACCESS -%>">
<input type="hidden" id="const_viewing" value="<%= Policy::VIEWING -%>">
<input type="hidden" id="const_downloading" value="<%= Policy::DOWNLOADING -%>">
<input type="hidden" id="const_editing" value="<%= Policy::EDITING -%>">


<%# this hidden input will submit all permission data with the form -%>
<%= hidden_field_tag "sharing[permissions][contributor_types]", "" -%>
<%= hidden_field_tag "sharing[permissions][values]", "" -%>

<%# @resource_type -%>
<%# @favourite_groups -%>
<%# @all_projects -%>

<%# @policy_type -%>
<%# @sharing_mode -%>
<%# @access_mode -%>
<%# @use_custom_sharing -%>
<%# @use_whitelist -%>
<%# @use_blacklist -%>


<div class="fold">

  <div class="foldTitle">
    <%#= help_icon("Here you can specify who can <b>view</b>, <b>download</b> and <b>edit</b> this #{@resource_type}.") -%>
    Sharing
  </div>

  <div class="foldContent" style="display: block;">

    <div class="box_infotext">
    	<p>
    		Here you can specify who can <b>view</b>, <b>download</b> and <b>edit</b> this <%= @resource_type -%>.
				<%= link_to_function "Click here" + expand_image, visual_effect(:toggle_blind, "more_info", :duration => 0.3) -%> for more information.
			</p>
			
			<div id="more_info" class="box_simple" style="display: none;">
              <p>
                Here you may specify the People, Projects or Groups that may View and Edit the details about your <%= @resource_type -%>.<br>
                <p>
                You may:
                <ul>
                  <li>Keep the <%= @resource_type -%> private, and only visible to yourself</li>
                  <li>Share with all MethodBox members</li>
                  <li>Define more fine-grained sharing, by selecting <em>Custom sharing permissions</em>.<br/> Using custom sharing permissions you can share with specific individuals,<br/>or set up custom favourite groups for people you regularly need to share with.</li>
                </ul>
                </p>
              </p>
			</div>
		</div>

		<br/>
		
		<!-- private -->
	  <p><label for="sharing_scope_<%= Policy::PRIVATE -%>">
    	<input type="hidden" name="sharing[access_type_<%= Policy::PRIVATE -%>]" value="<%= Policy::NO_ACCESS -%>">
			<input type="hidden" name="sharing[include_custom_sharing_<%= Policy::PRIVATE -%>]" value="0">
			
			<input <%= 'checked="checked"' if @sharing_mode == Policy::PRIVATE %> id="sharing_scope_<%= Policy::PRIVATE -%>" name="sharing[sharing_scope]"
            value="<%= Policy::PRIVATE -%>" type="radio" onclick="javascript:setSharingElementVisibility(<%= Policy::PRIVATE -%>);" />
        - Keep this <%= @resource_type -%> private.
		</label></p>
		
		
		<p style="font-weight: bold; margin-top: 1em;">Or share it with..</p>



    <!-- all registered users -->
    <p>
    	<label for="sharing_scope_<%= Policy::ALL_REGISTERED_USERS -%>">
      	<input type="radio" <%= 'checked="checked"' if @sharing_mode == Policy::ALL_REGISTERED_USERS -%> id="sharing_scope_<%= Policy::ALL_REGISTERED_USERS -%>" name="sharing[sharing_scope]"
              value="<%= Policy::ALL_REGISTERED_USERS -%>" onclick="javascript:setSharingElementVisibility(<%= Policy::ALL_REGISTERED_USERS -%>);" />
          - All registered users for
		  </label>
		  <%= render :partial => "assets/sharing_form_access_type_dropdown", :locals => { :access_type_selector_for_scope => Policy::ALL_REGISTERED_USERS,
			                                                                                :sharing_scope => @sharing_mode, :access_type => @access_mode } -%>
						
			<br/>
			<%= render :partial => "assets/sharing_form_custom_permissions_checkbox", :locals => { :custom_sharing_cb_for_scope => Policy::ALL_REGISTERED_USERS,
			                                                                                       :sharing_scope => @sharing_mode, :use_custom_sharing => @use_custom_sharing } -%>
		</p>


   <!-- custom permissions-->
    <p>
    	<label for="sharing_scope_<%= Policy::CUSTOM_PERMISSIONS_ONLY -%>">
      	<input type="radio" <%= 'checked="checked"' if @sharing_mode == Policy::CUSTOM_PERMISSIONS_ONLY -%> id="sharing_scope_<%= Policy::CUSTOM_PERMISSIONS_ONLY -%>" name="sharing[sharing_scope]"
              value="<%= Policy::CUSTOM_PERMISSIONS_ONLY -%>" onclick="javascript:setSharingElementVisibility(<%= Policy::CUSTOM_PERMISSIONS_ONLY -%>);" />
          - Use custom sharing permissions only
		  </label>
		</p>
		
		
		<div id="specific_sharing" style="display: <%= (@use_custom_sharing || @sharing_mode == Policy::CUSTOM_PERMISSIONS_ONLY) ? "block" : "none" -%>; margin-top: 2em;">
		  <p style="font-weight: bold;">
		  	Custom sharing permissions
			</p>
			
			<div class="box_editing" style="padding-left: 1em; font-size: 93%;">
				<p style="color: #666666;">
				  So far you have selected to share this <%= @resource_type -%> with:
				</p>
	      <p id="shared_with_list" class="box_editing_inner" style="line-height: 1.5;">
	        <span class="none_text" id="shared_with_text">Loading...</span>
	      </p>
				
				<p style="margin-top: 1em;">
					Build up the list of people and groups to share with.
				  Select from the options below and click "Add" to apply
					your choices and add collaborators to your current selection:
				</p>

				
				<!-- favourite groups -->
				<div class="fold" style="width: 70%; margin: 0.6em 0;">

				  <div class="foldTitle">
				    <%= image_tag method_to_icon_filename("spinner"), :id => "f_group_deleting_spinner", :alt=>"loading...", :title=>"loading...", :style => "display: none; vertical-align: middle; float: right;" -%>
						<%#= help_icon("Here you can choose your favourite groups to share this #{@resource_type} with.") -%>
				    Favourite Groups
				  </div>
				
				  <div class="foldContent" style="display: none; background-color: #F2F2F2;">
            <p style="margin: 1em 0 0 0; padding: 0; line-height: 1; text-align: center;">
              <% favourite_groups = FavouriteGroup.get_all_without_blacklists_and_whitelists(current_user.id) -%>
              <% f_group_select_options = (favourite_groups.blank? ? "<option value=\"\">You don't have any favourite groups</option>" : options_for_select(favourite_groups)) -%>
              
              <%= select_tag "favourite_group_select", f_group_select_options, :onchange => "javascript: replaceFavouriteGroupRedboxActionURL(); return(false);", :style => "width: 82%; vertical-align: middle; margin-right: 0.4em;", :disabled => (favourite_groups.blank?) -%>
              
              <%= link_to "Add", "", :id => "add_f_group_link", :style => "font-weight: bold; vertical-align: middle;", :disabled => (favourite_groups.blank?),
                  :onclick => "javascript: if($('favourite_group_select').disabled) return(false); addContributor('FavouriteGroup', $('favourite_group_select').options[$('favourite_group_select').selectedIndex].text, parseInt($('favourite_group_select').options[$('favourite_group_select').selectedIndex].value), #{Policy::DETERMINED_BY_GROUP}); return(false);" -%>
            </p>
            
            <ul style="text-align: left; margin: 1em 0 0.5em 3.2em;">
              <li id="edit_f_group_li" style="display: <%= favourite_groups.blank? ? "none" : "block" -%>; list-style-type: disc; padding: 0;"><%= favourite_group_popup_link_action_edit() -%></li>
              <li id="delete_f_group_li" style="display: <%= favourite_groups.blank? ? "none" : "block" -%>; list-style-type: disc; padding: 0;">
                <%= link_to "Delete selected favourite group", delete_favourite_group_path(0), :onclick => "deleteSelectedFavouriteGroup(); return(false);" -%>
              </li>
              <li id="create_f_group_li" style="list-style-type: disc; padding: 0.5em 0 0 0;"><%= favourite_group_popup_link_action_new() -%></li>
            </ul>
					</div>
				</div>
				

				
				<!-- individual people -->
        <div class="fold" style="width: 70%; margin: 0.6em 0;">

          <div class="foldTitle">
            <%#= help_icon("Here you can individual people to share this #{@resource_type} with.") -%>
            Individual People
          </div>
        
          <div class="foldContent" style="display: none; background-color: #F2F2F2; padding-bottom: 1.3em;">
			       <div id="facebook" class="clearfix">
	  
			          <p style="text-align: justify;">
			            Please type names of people to share this <%= @resource_type -%> with into the box below - suggestions will be displayed as you type.
			            Select access rights from drop-down menu; these will be applied to all chosen people.
			            Clicking "Add" will add selected people to the list. 
			          </p>
			          
			          <table>
			            <tr>
			              <td class="box_editing_inner">
			                <div tabindex="-1" id="ids" class="clearfix tokenizer" onclick="$('ip_autocomplete_input').focus();" style="width: 340px;">
			                  <span class="tokenizer_stretcher">^_^</span><span class="tab_stop"><input type="text" id="ip_hidden_input" tabindex="-1" ></span>
			                  
			                  <div id="ip_autocomplete_display" class="tokenizer_input">
			                     <input type="text" size="1" tabindex="" id="ip_autocomplete_input" />
			                  </div>
			                </div>
			                <div id="ip_autocomplete_populate" class="clearfix autocomplete typeahead_list" style="width: 343px; height: auto; overflow-y: hidden;display:none">
			                   <div class="typeahead_message">Type the name of a friend, friend list, or email address</div>                       
			                </div>
			              
			                <select name="individual_people_access_type_select" id="individual_people_access_type_select" style="width: 344px; vertical-align: middle; margin-top: 0.3em;">
			                  <option value="<%= Policy::NO_ACCESS -%>" >no access</option>
												<option value="<%= Policy::VIEWING -%>" >viewing only</option>
			                  <option value="<%= Policy::DOWNLOADING -%>" >viewing and downloading only</option>
			                  <option value="<%= Policy::EDITING -%>" >viewing, downloading and editing</option>
			                </select>
			              </td>
			              
			              <td style="padding-left: 0.7em; vertical-align: middle;">
			                <span style="font-weight: bold;"><%= link_to "Add", "", :onclick => "javascript: addIndividualPeople(); return(false);" -%></span>
			              </td>
			            </tr>
			          </table>
			      </div>
			      
			      
			      <script type="text/javascript">
			        var people_list = <%= @all_people_as_json -%>;
			        var prepopulate_with = [];
			      
							var individual_people_autocompleter = new Autocompleter.LocalAdvanced(
							    individual_people_autocompleter_id, 'ip_autocomplete_input', 'ip_autocomplete_display', 'ip_autocomplete_populate', people_list, prepopulate_with, {                                                  
				            frequency: 0.1,
				            updateElement: addAction,
				            search_field: "name",
				            hint_field: "email",
				            id_field: "id",
				            validation_type: "only_suggested"
			        });
			        var hidden_input = new HiddenInput('ip_hidden_input',individual_people_autocompleter);
							
							autocompleters[individual_people_autocompleter_id] = individual_people_autocompleter;
			      </script>
					</div>
				</div>
				
			</div>
		</div>
		
		
		<p style="font-weight: bold; margin-top: 1.5em;">Also include people in my..</p>
		
		<!-- whitelist / blacklist -->
		<p>
			<label for="cb_use_whitelist">
				<input <%= "disabled" if @sharing_mode == Policy::PRIVATE -%> style="margin-right: 0.3em;" value="1" type="checkbox" name="sharing[use_whitelist]" id="cb_use_whitelist" <%= "checked='checked'" if @use_whitelist -%> />
			  - "white list", who always get full access
			</label>
		  <% white_lst = current_user.get_whitelist -%>
		  <%= hidden_field_tag "#{FavouriteGroup::WHITELIST_NAME}_group_id", (white_lst ? white_lst.id : "none") -%>
			<span>&nbsp;&nbsp;[ 
				<span id="<%= FavouriteGroup::WHITELIST_NAME -%>_edit_span" style="display: <%= white_lst ? "inline" : "none" -%>;">
					<%= link_to "edit", "/", :onclick => "editWhitelistBlacklist('#{FavouriteGroup::WHITELIST_NAME}'); return(false);" -%>
				</span>
				<% unless white_lst -%>
				  <span id="<%= FavouriteGroup::WHITELIST_NAME -%>_create_span">
					  <%= link_to "create", "/", :onclick => "createWhitelistBlacklist('#{FavouriteGroup::WHITELIST_NAME}'); return(false);" -%>
					</span>
				<% end -%>
			 ]</span>
			 <%= image_tag method_to_icon_filename("spinner"), :id => "#{FavouriteGroup::WHITELIST_NAME}_creation_spinner", :alt=>"loading...", :title=>"loading...", :style => "display: none; vertical-align: middle;" -%>
		</p>
		<p>
			<label for="cb_use_blacklist">
				<input <%= "disabled" if @sharing_mode == Policy::PRIVATE -%> style="margin-right: 0.3em;" value="1" type="checkbox" name="sharing[use_blacklist]" id="cb_use_blacklist" <%= "checked='checked'" if @use_blacklist -%> />
			  - "black list", who have no access at all
			</label>
		  <% black_lst = current_user.get_blacklist -%>
		  <%= hidden_field_tag "#{FavouriteGroup::BLACKLIST_NAME}_group_id", (black_lst ? black_lst.id : "none") -%>
			<span>&nbsp;&nbsp;[ 
				<span id="<%= FavouriteGroup::BLACKLIST_NAME -%>_edit_span" style="display: <%= black_lst ? "inline" : "none" -%>;">
					<%= link_to "edit", "/", :onclick => "editWhitelistBlacklist('#{FavouriteGroup::BLACKLIST_NAME}'); return(false);" -%>
				</span>
				<% unless black_lst -%>
				  <span id="<%= FavouriteGroup::BLACKLIST_NAME -%>_create_span">
					  <%= link_to "create", "/", :onclick => "createWhitelistBlacklist('#{FavouriteGroup::BLACKLIST_NAME}'); return(false);" -%>
					</span>
				<% end -%>
			 ]</span>
			 <%= image_tag method_to_icon_filename("spinner"), :id => "#{FavouriteGroup::BLACKLIST_NAME}_creation_spinner", :alt=>"loading...", :title=>"loading...", :style => "display: none; vertical-align: middle;" -%>
	  </p>
		<div style="display: none;">
		  <%# this DIV is to hold the 'redbox' links generated by a method in application_helper, -%>
		  <%# these will be "clicked" by javascript, because hidden divs that are present in the -%>
		  <%# generated code causes IE to break layout by inserting extra line break otherwise -%>
			<div id="<%= FavouriteGroup::WHITELIST_NAME -%>_redbox_link_div">
  			<%= whitelist_blacklist_edit_popup_link(FavouriteGroup::WHITELIST_NAME) -%>
			</div>
			<div id="<%= FavouriteGroup::BLACKLIST_NAME -%>_redbox_link_div">	
  			<%= whitelist_blacklist_edit_popup_link(FavouriteGroup::BLACKLIST_NAME) -%>
			</div>
		</div>
		
  </div>
</div>

<script type="text/javascript">
	init_sharing();
	
	GET_POLICY_DEFAULTS_LINK = '<%= request_policy_settings_path -%>';
  GET_INSTITUTIONS_LINK = '<%= request_institutions_projects_path -%>';

	
	// CREATE_FAVOURITE_GROUP_LINK = '<%= create_favourite_group_path -%>';
	// UPDATE_FAVOURITE_GROUP_LINK = '<%= update_favourite_group_path -%>';
	
	
	
	<% @policy.permissions.each do |p| -%>
	  <% case p.contributor_type; when "FavouriteGroup", "Person", "Project", "Institution", "WorkGroup" -%>
		  <#% unless p.contributor_type == "FavouriteGroup" && [FavouriteGroup::WHITELIST_NAME, FavouriteGroup::BLACKLIST_NAME].include?(p.contributor.name) -%>
			  if('<%= p.contributor_type -%>' in permissions_for_set)
				  permissions_for_set['<%= p.contributor_type -%>']++;
			  else {
					permissions_for_set['<%= p.contributor_type -%>'] = 0;
					permission_settings['<%= p.contributor_type -%>'] = new Array();
				}
				
				<% if p.contributor_type == "Person" -%>
				  <% contributor_name = (p.contributor.first_name + " " + p.contributor.last_name) -%>
				<% elsif p.contributor_type == "WorkGroup" -%>
				  <% contributor_name = p.contributor.project.name + " @ " + p.contributor.institution.name -%>
				<% else -%>
				  <% contributor_name = p.contributor.name -%>
				<% end -%>
				
				// add current values into the associative array of permissions:
        // first index is the category of contributor type of the permission, the second - consecutive
        // number of occurrences of permissions for such type of contributor
				permission_settings['<%= p.contributor_type -%>'][permissions_for_set['<%= p.contributor_type -%>']] =
				  ['<%= contributor_name -%>', <%= p.contributor_id -%>, <%= p.access_type -%>];
			<% end -%>
		<% end -%>
	<#% end -%>
	
	// build custom permissions list and set relevant other options
	updateCustomSharingSettings();
</script>